<template>
	<view class="p-20">
		<view class="bg-white p-30 border-radius">
			<view class="form-item mb-40">
				<view class="font-bold font-28">工作性质</view>
				<view class="mt-20">
					<uni-data-checkbox v-model="form.jobNature" :localdata="job_nature" />
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">工作城市</view>
				<view class="mt-20">
					<uni-data-select v-model="form.city" :localdata="city_enum" @change="change"></uni-data-select>
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">职业名称</view>
				<view class="mt-20">
					<uni-easyinput v-model="form.name" placeholder="请输入职业名称" />
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">职业类别</view>
				<view class="mt-20">
					<uni-data-select v-model="form.postion" :localdata="postion_category"
						@change="change"></uni-data-select>
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">职业描述</view>
				<view class="mt-20">
					<uni-easyinput type="textarea" v-model="form.info" placeholder="请输入职业描述" />
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">学历要求</view>
				<view class="mt-20">
					<uni-data-select v-model="form.education" :localdata="education_enum"
						@change="change"></uni-data-select>
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">经验要求</view>
				<view class="mt-20">
					<uni-data-select v-model="form.experience" :localdata="experience_enum"
						@change="change"></uni-data-select>
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">最低薪资</view>
				<view class="mt-20">
					<uni-easyinput v-model="form.payLow" placeholder="请输入最低薪资" />
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">最高薪资</view>
				<view class="mt-20">
					<uni-easyinput v-model="form.payHigh" placeholder="请输入最高薪资" />
				</view>
			</view>
			<view class="form-item mb-40">
				<view class="font-bold font-28">招聘人数</view>
				<view class="mt-20">
					<uni-easyinput v-model="form.peopleNum" placeholder="请输入招聘人数" />
				</view>
			</view>
		</view>

		<view class="bottom-view flex flex-colum flex-center">
			<view class="bottom-btn font-white bg-g" @click="submit">发布职位</view>
			<view class="bottom-btn font-white bg-red mt-20" @click="del" v-if="showDel">删除</view>
		</view>
	</view>
</template>

<script>
	import {
		add,
		edit,
		del
	} from '../../api/invite.js';
	import {
		getCategory
	} from '../../api/common.js';

	import {
		find
	} from '../../api/company';
	import {
		getInfo
	} from '../../api/hr_user.js';
	export default {
		data() {
			return {
				job_nature: [{
					text: '全职',
					value: '全职'
				}, {
					text: '兼职',
					value: '兼职'
				}, {
					text: '实习',
					value: '实习'
				}, {
					text: '校园',
					value: '校园'
				}],
				education_enum: [{
					text: '初中及以下',
					value: '初中及以下'
				}, {
					text: '中专/中技',
					value: '中专/中技'
				}, {
					text: '高中',
					value: '高中'
				}, {
					text: '大专',
					value: '大专'
				}, {
					text: '本科',
					value: '本科'
				}, {
					text: '硕士',
					value: '硕士'
				}, {
					text: '博士',
					value: '博士'
				}],
				experience_enum: [{
					text: '实习',
					value: '实习'
				}, {
					text: '1年以内',
					value: '1年以内'
				}, {
					text: '1-3年',
					value: '1-3年'
				}, {
					text: '3-5年',
					value: '3-5年'
				}, {
					text: '5年以上',
					value: '5年以上'
				}],
				city_enum: [{
					text: '东营市',
					value: '东营市'
				},{
					text: '东营区',
					value: '东营区'
				}, {
					text: '河口区',
					value: '河口区'
				}, {
					text: '垦利区',
					value: '垦利区'
				}, {
					text: '广饶县',
					value: '广饶县'
				}, {
					text: '利津县',
					value: '利津县'
				}],
				form: {
					status: 1
				},
				postion_category: [],
				showDel: false
			};
		},
		onLoad() {
			this.getCate1()
			this.getCompany()
			const info = uni.getStorageSync('info')
			if (info) {
				this.form = info
				this.showDel = true
			}
		},
		onUnload() {
			uni.removeStorageSync('info')
		},
		methods: {
			getHr() {
				const _this = this
				getInfo().then(res => {
					if (res.code !== 200) {
						this.$tips.showConfirmModal('您还未填写HR信息，请填写后添加职位', () => {
							uni.navigateBack()
						})
					} 
				})
			},
			del() {
				const _this = this
				this.$tips.showModal('是否删除该职位？', () => {
					del({
						id: this.form.id
					}).then(res => {
						if (res.code === 200) {
							_this.$tips.showConfirmModal('删除成功', () => {
								uni.navigateBack()
							})
						} else {
							_this.$tips.showConfirmModal('删除失败', () => {})
						}
					})
				}, () => {})
			},
			getCompany() {
				const _this = this
				find().then(res => {
					if (res.code === 200) {
						this.form.companyId = res.result.id
						this.getHr()
					} else {
						_this.$tips.showConfirmModal('您还未填写公司信息，请填写后添加职位', () => {
							uni.navigateBack()
						})
					}
				})
			},
			getCate1() {
				getCategory({
					pcode: 'postion_category'
				}).then(res => {
					const postion_category = res.result
					for (let x in postion_category) {
						this.postion_category.push({
							value: postion_category[x].title,
							text: postion_category[x].title,
						})
					}
				})
			},
			submit() {
				const _this = this
				this.$tips.showModal('是否提交职位信息？', () => {
					if (uni.getStorageSync('info')) {
						edit({
							...this.form
						}).then(res => {
							if (res.code === 200) {
								_this.$tips.showConfirmModal('提交成功，请等待审核', () => {
									uni.navigateBack()
								})
							} else {
								_this.$tips.showConfirmModal('提交失败，请重试', () => {})
							}
						})
					} else {
						add({
							...this.form
						}).then(res => {
							if (res.code === 200) {
								_this.$tips.showConfirmModal('提交成功，请等待审核', () => {
									uni.navigateBack()
								})
							} else {
								_this.$tips.showConfirmModal('提交失败，请重试', () => {})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #f6f6f6;
		padding-bottom: 200rpx;
	}

	.bottom-view {
		width: 100vw;
		padding: 40rpx 0;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99999;
		border-top: 1px solid #efefef;

		.bottom-btn {
			width: 80vw;
			text-align: center;
			border-radius: 50rpx;
			padding: 20rpx 0;
		}
	}
</style>